#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Object
{
	int weight;
	int price;
};

struct Node
{
	int total_price;
	bool arrival;

	Node()
	{
		total_price = 0;
		arrival = false;
	}
};

int main()
{
	int task_count = 0;
	cin >> task_count;
	while( task_count-- )
	{
		int object_count = 0;
		cin >> object_count;
		vector<Object> objects( object_count );
		for( int i = 0; i < object_count; i++ )
		{
			cin >> objects[i].price >> objects[i].weight;
		}
		int people_count = 0;
		cin >> people_count;
		vector<int> peoples( people_count );
		int max_people_weight = -1;
		for( int i = 0; i < people_count; i++ )
		{
			int weight = 0;
			cin >> weight;
			if( max_people_weight < weight )
			{
				max_people_weight = weight;
			}
			peoples[i] = weight;
		}

		vector<Node> F( max_people_weight + 1 );
		F[0].arrival = true;
		int max = 0;
		for( vector<Object>::iterator it = objects.begin(); it != objects.end(); it++ )
		{
			for( int i = max; i >= 0; i-- )
			{
				if( !F[i].arrival ) continue;
				int k = i + it->weight;
				if( k > max_people_weight ) continue;
				if( F[k].total_price >= F[i].total_price + it->price ) continue;
				F[k].total_price = F[i].total_price + it->price;
				F[k].arrival = true;
				if( max < k ) max = k;
			}
		}

		vector<int> G( max_people_weight + 1 );
		int max_price = 0;
		for( int i = 1; i <= max_people_weight; i++ )
		{
			if( max_price < F[i].total_price )
			{
				max_price = F[i].total_price;
			}
			G[i] = max_price;
		}

		int max_total_price = 0;
		for( vector<int>::iterator it = peoples.begin(); it != peoples.end(); it++ )
		{
			max_total_price += G[*it];
		}

		cout << max_total_price << endl;
	}
	return 0;
}
